package com.hnxxxy.rewardmgr.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnxxxy.rewardmgr.bean.NumberConfig;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @projectName:reward_pro
 * @see:com.hnxxxy.rewardmgr.utils
 * @author:张屹东
 * @createTime:2020/12/26 14:18
 */
public class UploadUtils {
    // 项目根路径下的目录  -- SpringBoot static 目录相当于是根路径下（SpringBoot 默认）
    public final static String IMG_PATH_PREFIX = "static/upload";
    public final static String IMG_PATH = "static";

    public static File getImgDirFile(String pathUrl, Object... path) {
        String fileDirPath = null;
        if (pathUrl.equals("img")) {
            // 构建上传文件的存放 "文件夹" 路径
            fileDirPath = new String("src/main/resources/" + IMG_PATH);
        } else {
            fileDirPath = new String("src/main/resources/" + IMG_PATH_PREFIX);
        }
        File fileDir = new File(fileDirPath);
       /* if (!fileDir.exists()) {
            // 递归生成文件夹
            fileDir.mkdirs();
        }*/
        return fileDir;
    }


    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
